package com.sxj.corejava.code08_常用类.object类;

/**
 * @author 石小俊
 * 2024年07月19日 9:18
 */
public class Test02_常用方法 {

    public static void main(String[] args) throws CloneNotSupportedException {
        //getClassTest();
        //equalsTest();
        //hashCodeTest();
        //toStringTest();
        //cloneTest();
        finalizeTest();
    }

    public static void getClassTest() {
        Person person = new Person();
        System.out.println(person.getClass());
        Person person2 = new Man();
        System.out.println(person2.getClass());
        Person person3 = new WoMan();
        System.out.println(person3.getClass());

        if (person2 instanceof Man) {
            Man man = (Man) person2;
            man.work();
        }

        System.out.println(Person.class);
        System.out.println(Man.class);
        System.out.println(WoMan.class);

        if (person3.getClass() == WoMan.class) {
            WoMan woMan = (WoMan) person3;
            woMan.shopping();
        }

    }

    public static void equalsTest() {
        Person person = new Person("admin", 18);
        Person person2 = new Person("admin", 19);
        System.out.println(person == person2);      // false
        // 默认情况下,Object中equals方法就是==
        // 因此,此处默认也是false
        // 但是,我们可以选择重写equals方法,根据需求设定比较的规则
        // 此处我们重写的方法是比较了Person对象中的name与age的值的大小
        // 如果两者值一致,则表示相等
        System.out.println(person.equals(person2));
        int i1 = 1;
        int i2 = 1;
        System.out.println(i1 == i2);

        // 在字符串中,重写了Object提供的equals方法
        // 该方法在字符串值比较值的大小,不关心地址
        String s1 = "admin";
        String s2 = "admin";
        System.out.println(s1.equals(s2));
        String s3 = new String("admin");
        String s4 = new String("admin");
        System.out.println(s3.equals(s4));

        Man man = new Man("admin", 18);
        Man man2 = new Man("admin", 18);
        System.out.println(man.equals(man2));
    }

    public static void hashCodeTest() {
        Person person = new Person("admin", 18);
        System.out.println(person);
        Person person2 = new Person("admin", 18);
        System.out.println(person2);
        Person person3 = new Person("admin", 18);
        System.out.println(person.hashCode());
        System.out.println(person2.hashCode());
        System.out.println(person3.hashCode());
    }

    public static void toStringTest() {
        Person person = new Person("admin", 18);
        System.out.println(person);
    }

    public static void cloneTest() throws CloneNotSupportedException {
        Person person = new Person("admin", 18);
        //Person person2 = (Person) person.clone();
        //System.out.println(person2);

        Address address = new Address("江苏", "南京");
        person.setAddress(address);
        System.out.println(person);

        // 该克隆属于浅克隆
        Person person2 = (Person) person.clone();
        System.out.println(person2);

        person2.setName("alice");
        person2.setAge(20);
        person2.getAddress().setProvince("安徽");
        person2.getAddress().setCity("合肥");
        System.out.println(person);
        System.out.println(person2);

    }

    public static void finalizeTest(){
        Person person = new Person();
        Person person2 = person;
        person = null;
        person2 = null;

        // 建议gc进行垃圾回收
        System.gc();
        //Runtime.getRuntime().gc();
    }

}
